<template>
  <section class="my-runleg">
    <div
      class="task"
      @click="routerTo(item.orderId)"
      v-for="item in list"
      :key="item"
    >
      <div class="top">
        <div class="flex flex-middle flex-between">
          <div class="flex flex-middle">
            <img :src="item.headImgUrl" />
            <div>
              <div class="title">{{item.taskName}}</div>
              <div class="mt10">
                <span class="baochou">报酬</span>
                <span class="price">+{{item.orderAmt}}元</span>
              </div>
            </div>
          </div>
          <button v-if="item.orderStatus == 1">待接单</button>
          <button v-else-if="item.orderStatus == 2">已接单</button>
          <button v-else-if="item.orderStatus == 3">开始任务</button>
          <button v-else-if="item.orderStatus == 4">完成任务</button>
          <button v-else-if="item.orderStatus == 5">用户确认完成</button>
          <button v-else-if="item.orderStatus == 6">任务已评价</button>
        </div>
        <div class="bottom flex">
          <van-icon
            name="underway-o"
            class="icon"
          />
          <span>下单时间: {{item.orderTime}}</span>
        </div>
      </div>
      <div class="flex bottom flex-main-end">
        <button
          class="btn1"
          v-if="item.orderStatus == 4"
          @click.stop="makeMyTaskOrder(item.orderId,1)"
        >确认完成</button>
        <button
          class="btn2"
          v-if="item.orderStatus == 1 || item.orderStatus == 5"
          @click.stop="makeMyTaskOrder(item.orderId,2)"
        >删除</button>
        <button
          class="btn3"
          v-if="item.orderStatus == 2"
          @click.stop="makeMyTaskOrder(item.orderId,3)"
        >取消</button>
      </div>
    </div>
    <div class="footer">
      <button
        class="release-task"
        @click="routerTo(1)"
      >
        <van-icon
          custom-class="van-icon"
          name="plus"
          color="white"
        />
        <span>发布任务</span>
      </button>
    </div>
  </section>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      pageNum: 1,
      optionKeys: {
        1: {
          title: '确认完成',
          desc: '您确定完成吗？'
        },
        2: {
          title: '删除',
          desc: '您确定删除吗？'
        },
        3: {
          title: '取消',
          desc: '您确定取消吗'
        },
      }
    };
  },
  onShow() {
    this.list = [];
    this.selectMyOrderList(1);
  },
  onReachBottom() {
    this.selectMyOrderList(this.pageNum + 1);
  },
  methods: {
    routerTo(id) {
      if (id == 1) {
        wx.navigateTo({ url: "../releasetask/main?id=" + id });
      } else {
        wx.navigateTo({ url: "../releasetaskdetail/main?id=" + id });
      }
    },
    selectMyOrderList(pageNum) {
      this.$api.selectMyOrderList({ pageNum, pageSize: 10 }).then(res => {
        if (res.data.taskMyOrderList.length > 0) {
          let list = res.data.taskMyOrderList.map(item => {
            item.orderTime = this.formatDate(item.orderTime, "Y-M-D h:s");
            return item;
          });
          if (pageNum > 1) {
            this.list = this.list.concat(list);
          } else {
            this.list = list;
          }
          this.pageNum = pageNum;
        }
      });
    },
    //操作任务1.确认完成 2.删除 3.取消
    makeMyTaskOrder(orderId, type) {
      const self = this;
      const obj = this.optionKeys[type];
      wx.showModal({
        title: obj.title, //提示的标题,
        content: obj.desc, //提示的内容,
        showCancel: true, //是否显示取消按钮,
        cancelText: "取消", //取消按钮的文字，默认为取消，最多 4 个字符,
        cancelColor: "#000000", //取消按钮的文字颜色,
        confirmText: "确定", //确定按钮的文字，默认为取消，最多 4 个字符,
        confirmColor: "#3BB8FD", //确定按钮的文字颜色,
        success: res => {
          if (res.confirm) {
            self.$api.makeMyTaskOrder({ orderId, type }).then(res => {
              if (res.code == 0) {
                wx.showToast({
                  title: '操作成功', //提示的内容,
                  icon: "none", //图标,
                  duration: 2000, //延迟时间,
                  mask: true, //显示透明蒙层，防止触摸穿透,
                  success: res => {
                    self.selectMyOrderList(1);
                  }
                });
              } else {
                wx.showToast({
                  title: res.msg, //提示的内容,
                  icon: "none", //图标,
                  duration: 2000, //延迟时间,
                  mask: true, //显示透明蒙层，防止触摸穿透,
                  success: res => {}
                });
              }
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.my-runleg {
  padding-bottom: 60px;
  .task {
    background: white;
    padding: 15px;
    margin: 15px;
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    .top {
      padding-bottom: 15px;
      border-bottom: 1px solid #f5f4f7;
      img {
        width: 40px;
        height: 40px;
        border-radius: 100%;
        margin-right: 15px;
      }
      .title {
        font-size: 13px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(46, 46, 46, 1);
      }
      .baochou {
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(193, 196, 201, 1);
        margin-right: 10px;
      }
      .price {
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(59, 184, 253, 1);
      }
      button {
        margin: 0;
        padding: 0 8px;
        height: 21px;
        line-height: 21px;
        border: 0.5px solid rgba(59, 184, 253, 1);
        border-radius: 11px;
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(59, 184, 253, 1);
        span {
          margin-left: 10px;
        }
      }
      .bottom {
        padding-top: 15px;
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
        color: rgba(193, 196, 201, 1);
      }
    }
    .bottom {
      padding-top: 15px;
      & > button:not(:last-child) {
        margin-right: 15px;
      }
      button {
        width: 60px;
        height: 21px;
        background: rgba(59, 184, 253, 1);
        border-radius: 2px;
        font-size: 12px;
        font-family: PingFang;
        font-weight: 500;
        line-height: 21px;
        margin: 0;
        padding: 0;
        text-align: center;
      }
      .btn1,
      .btn2 {
        border: 1px solid rgba(59, 184, 253, 1);
        background: white;
        color: rgba(59, 184, 253, 1);
      }
      .btn3 {
        color: rgba(255, 255, 255, 1);
      }
    }
  }
  .footer {
    width: 100%;
    background: #f7f7f7;
    height: 60px;
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 99;
    .release-task {
      width: 345px;
      background: rgba(59, 184, 253, 1);
      color: white;
      border-radius: 5px;
      padding: 0;
      text-align: center;
    }
  }
}
</style>
<style lang="scss">
page {
  background: #f7f7f7;
}
.release-task {
  .van-icon {
    position: relative;
    top: 2px;
  }
}
</style>